//
//  ViewController.m
//  Study
//
//  Created by 振兴 刘 on 2017/7/6.
//  Copyright © 2017年 振兴 刘. All rights reserved.
//

#import "ViewController.h"
#import <YTKNetwork/YTKNetwork.h>
#import "RequestUnit.h"
#import <CocoaAsyncSocket/GCDAsyncSocket.h>
#import "YMSocketUtils.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <XMLReader/XMLReader.h>
#import "Sort.h"
#import <FBRetainCycleDetector/FBRetainCycleDetector.h>
#import "CTMediator+CTMediatorTest.h"
#import <MGJRouter/MGJRouter.h>

typedef void (^textBlock) (NSString* first, NSString* second);

@interface ViewController () <GCDAsyncSocketDelegate>

@property(nonatomic, strong) GCDAsyncSocket* mClinetSocket;

@property(nonatomic, strong) IBOutlet UITextField* mUserTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated {
    
    NSString *homeDir = NSHomeDirectory();
    NSLog(@"沙盒地址: %@", homeDir);
    
    //[self reactiveCocoa];
    //[self XMLReader];
    //[self http];
    //[self socket];
    
    //[self sort];
    
//    FBRetainCycleDetector* detector = [[FBRetainCycleDetector alloc] init];
//    [detector addCandidate:self.delegate];
//    //NSSet *retainCycles = [detector findRetainCycles];
//    //NSLog(@"%@", retainCycles);
//
//    NSSet<NSArray<FBObjectiveCGraphElement *> *> *retainCycles = [detector findRetainCycles];
//    NSLog(@"%@", retainCycles);
//
//    [self actionTestBlock:^(NSString *first, NSString *second) {
//
//    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reactiveCocoa {
    
    [[self.mUserTextField.rac_textSignal filter:^BOOL(id value) {
        NSString*text = value;
        return text.length > 5;
        
    }] subscribeNext:^(id x) {
        NSLog(@"input:%@", x);
        
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
    
    RAC(self.mUserTextField, backgroundColor) = [self.mUserTextField.rac_textSignal map:^id(id value) {
        
        NSString* v = value;
        return v.length < 5 ? [UIColor redColor]:[UIColor greenColor];
    }];
}

- (void)XMLReader {
    
    NSString* xmlPath = [[NSBundle mainBundle] pathForResource:@"xmlreader" ofType:@"xml"];
    NSData* xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSDictionary* xmlDic = [XMLReader dictionaryForXMLData:xmlData error:nil];
    NSLog(@"XMLReader:%@", xmlDic);
}

- (void)http {
    
    RequestUnit* request = [[RequestUnit alloc]initRequest:@"" password:@""];
    [request startWithCompletionBlockWithSuccess:^(YTKBaseRequest * request) {
        
        int i = 0;
        i++;
        
    }failure:^(YTKBaseRequest * request) {
        
        NSString* aa = request.responseString;
        int i = 0;
        i++;
    }];
}

- (void)socket {
    
    self.mClinetSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    
    [self.mClinetSocket connectToHost:@"60.191.203.119" onPort:2021 withTimeout:-1 error:nil];
    
    //[sock connectToHost:@"www.baidu.com" onPort:80 error:nil];
    
    //[sock writeData:[@"GET / HTTP/1.1\n\n" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:3 tag:1];
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
    
    [self.mClinetSocket readDataWithTimeout:-1 tag:1];
}

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {

    NSLog(@"did read data");
    NSString* message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"message is: \n%@",message);
    
    [self.mClinetSocket readDataWithTimeout:-1 tag:1];
}

- (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
    

}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {

}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(nullable NSError *)err
{

}

- (IBAction)sendData:(id)sender {
    
    Byte time[6];
    time[0] = 17;
    time[1] = (Byte)7;
    time[2] = (Byte)17;
    time[3] = (Byte)16;
    time[4] = (Byte)12;
    time[5] = (Byte)40;
    
    NSMutableData *sendData = [NSMutableData new];
    [sendData appendData:[YMSocketUtils bytesFromUInt16:100]];
    [sendData appendData:[YMSocketUtils bytesFromUInt16:70]];
    [sendData appendData:[YMSocketUtils bytesFromUInt32:50]];
    [sendData appendData:[@"865188030001744" dataUsingEncoding:NSUTF8StringEncoding]];
    [sendData appendData:[@"134566" dataUsingEncoding:NSUTF8StringEncoding]];
    [sendData appendData:[YMSocketUtils bytesFromUInt32:-123]];
    [sendData appendData:[YMSocketUtils bytesFromUInt32:88]];
    [sendData appendData:[YMSocketUtils byteFromUInt8:1]];
    [sendData appendData:[@"GROVCNOOX1000PX1706250XXXXXXXX\0" dataUsingEncoding:NSUTF8StringEncoding]];
    [sendData appendData:[NSData dataWithBytes:time length:6]];
    
    [self.mClinetSocket writeData:sendData withTimeout:-1 tag:1];
}

- (void)sort {
    NSMutableArray* array = [[NSMutableArray alloc] initWithObjects:@(8),@(2), @(5), @(4), @(9), @(2), @(6), @(7), @(3), @(0), @(1),@(-1), nil];
    //[Sort bubblingSort:array];
    //[Sort insetSort:array];
    //[Sort heapSort:array];
    [Sort quickSort:array];
    
    for (int i = 0; i < array.count; i++) {
        NSLog(@"%@", array[i]);
    }
}

- (void)actionTestBlock:(textBlock)fun {
    
    
}

- (IBAction)gotoCTMediatorTest:(id)sender {
    
    UIViewController* view = [[CTMediator sharedInstance] pushView];
    [self.navigationController pushViewController:view animated:YES];
}

- (IBAction)gotoMGJRouterTest:(id)sender {
    
    [MGJRouter openURL:@"Router://PushTestVC"
          withUserInfo:@{@"navigationVC" : self.navigationController
                         }
            completion:nil];
}

@end
